package com.jn.erp.web.api.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.security.entity.SysOperLog;
import com.jn.erp.security.sysservice.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author jn
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseWebAuthController {
    @Autowired
    private SysOperLogService operLogService;

//        @PreAuthorize("@ss.hasPermi('monitor:operlog:query')")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog) {
        String orgId = loginDeptAuth();
        operLog.setOrgId(orgId);
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }


    @Log(title = "操作日志删除", businessType = BusinessType.DELETE)
//    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds) {
        return toAjax(operLogService.removeByIds(Arrays.asList(operIds)));
    }

    @Log(title = "操作日志清空", businessType = BusinessType.CLEAN)
//    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        String orgId = loginDeptAuth();
        QueryWrapper<SysOperLog> qw = new QueryWrapper();
        qw.apply("1=1");
        if(!StrUtil.equals(orgId,"1")){
            qw.eq("org_id",orgId);
        }
        operLogService.remove(qw);
        return success();
    }
}
